/*
	Copyright 2007-2010 Petr Ohlidal

	This file is a part of GrobMaker

	GrobMaker is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	GrobMaker is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with GrobMaker.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GROBMAKER_AREAFINDER
#define GROBMAKER_AREAFINDER

/** @file */

#include "../Global.hpp"
#include "../Util/Area.hpp"
#include "../Images/Pixel.hpp"

namespace GrobMaker{

/**
* @brief Finds an area of a bitmap with specified parameters
*/
class AreaFinder{
	Pixel max,min;
	Pixel active; ///< A mask that determines which pixels are checked.

	public:

	AreaFinder(const Pixel& min_, const Pixel& max_, const Pixel& active_):
		max(max_), min(min_), active(active_){}

	bool check(BYTE* b) const{
		bool rgbaOk[] = {active.r==0,active.g==0,active.b==0,active.a==0};
		// Red
		if( this->active.r>0 && b[FI_RGBA_RED]>=this->min.r && b[FI_RGBA_RED]<=this->max.r ){
			rgbaOk[0]=true;
		}
		// Green
		if( this->active.g>0 && b[FI_RGBA_GREEN]>=this->min.g && b[FI_RGBA_GREEN]<=this->max.g ){
			rgbaOk[1]=true;
		}
		// Blue
		if( this->active.b>0 && b[FI_RGBA_BLUE]>=this->min.b && b[FI_RGBA_BLUE]<=this->max.b ){
			rgbaOk[2]=true;
		}
		// Alpha
		if( this->active.a>0 && b[FI_RGBA_ALPHA]>=this->min.a && b[FI_RGBA_ALPHA]<=this->max.a ){
			rgbaOk[3]=true;
		}
		return rgbaOk[0] && rgbaOk[1] && rgbaOk[2] && rgbaOk[3];
	}
};

} // namespace GrobMaker

#endif //#ifndef GROBMAKER_AREAFINDER
